package com.xiangxiao.rpan.settings.controller;

import com.xiangxiao.rpan.settings.constant.ResponseCode;
import com.xiangxiao.rpan.settings.constant.ResponseMessage;
import com.xiangxiao.rpan.settings.dto.InstallSystemDTO;
import com.xiangxiao.rpan.settings.dto.JsonObject;
import com.xiangxiao.rpan.settings.dto.SystemConfigDTO;
import com.xiangxiao.rpan.settings.dto.UpdateSiteSettingDto;
import com.xiangxiao.rpan.settings.exception.SettingsException;
import com.xiangxiao.rpan.settings.service.SystemConfigService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @auther xiangxiao
 * @email 573768011@qq.com
 * @data 2023/7/4 15:13
 */
@Slf4j
@RestController
@RequestMapping("/admin")
public class LoginController {
  private static Logger logger = LoggerFactory.getLogger(LoginController.class);
  @Autowired
  private SystemConfigService systemConfigService;

  @GetMapping("/login/verify-mode")
  public JsonObject loginVerifyMode() {
    try {
      SystemConfigDTO systemConfig = systemConfigService.getSystemConfig();
      return new JsonObject(ResponseMessage.QUERY_LOGIN_VERIFY_MODE_SUECCESS, ResponseCode.OK, systemConfig.getLoginVerifyMode());
    } catch (SettingsException e) {
      logger.error(ResponseMessage.QUERY_LOGIN_VERIFY_MODE_FAILED, e);
      return new JsonObject(ResponseMessage.QUERY_LOGIN_VERIFY_MODE_FAILED, ResponseCode.QUERY_LOGIN_VERIFY_MODE_FAILED);
    }
  }

  @GetMapping("/config")
  public JsonObject getConfig() {
    try {
      SystemConfigDTO systemConfigDTO = systemConfigService.getSystemConfig();
      return new JsonObject(ResponseMessage.QUERY_CONFIG_SUECCESS, ResponseCode.OK, systemConfigDTO);
    } catch (SettingsException e) {
      logger.error(ResponseMessage.QUERY_CONFIG_FAILED, e);
      return new JsonObject(ResponseMessage.QUERY_CONFIG_FAILED, ResponseCode.QUERY_CONFIG_FAILED);
    }

  }

  @PutMapping("/config/site")
  public JsonObject ModifySiteConfig(@Valid @RequestBody UpdateSiteSettingDto settingsDto) {
    try {
      SystemConfigDTO systemConfigDTO = new SystemConfigDTO();
      BeanUtils.copyProperties(settingsDto, systemConfigDTO);
      systemConfigService.updateSystemConfig(systemConfigDTO);
      return new JsonObject(ResponseMessage.MODIFY_SETTINS_CONFIG_SUECCESS, ResponseCode.OK, true);
    } catch (SettingsException e) {
      logger.error(ResponseMessage.MODIFY_SETTINS_CONFIG_FAILED, e);
      return new JsonObject(ResponseMessage.MODIFY_SETTINS_CONFIG_FAILED, ResponseCode.MODIFY_SETTINS_CONFIG_FAILED);
    }

  }
}
